<template>
  <div class="login">
    <div class="header">
      <div>
        <img src="../assets/images/aijiale-logo.png" alt="" />
        艾家乐设计管理平台
      </div>
      <div>你好，欢迎使用艾家乐设计管理平台</div>
    </div>
    <div class="main">
      <div class="form">
        <h2>用户注册</h2>
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          status-icon=""
          label-width="60px"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model.trim="form.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model.trim="form.password"
              show-password
            />
          </el-form-item>
          <el-form-item label="重复" prop="chkpassword">
            <el-input
              type="password"
              v-model.trim="form.chkpassword"
              show-password
            />
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model.trim="form.mobile" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">注册</el-button>
            <el-button type="danger" @click="$router.push('/login')"
              >返回</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="footer">
      <div>
        <img src="../assets/images/C.png" alt="" />
        2021 艾登软件（上海）股份有限公司版权所有
      </div>
      <div>
        <img src="../assets/images/copyright.png" alt="" />
        ICP证：沪ICP备14033657号-3
      </div>
      <div>
        <img src="../assets/images/networkSecurity.png" alt="" />
        沪公网安备 31011502008722号
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";
export default {
  data() {
    // 验证用户名
    const checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入用户名"));
      } else if (!/^[a-zA-Z]\w{3,15}$/.test(value)) {
        return callback(new Error("请输入有效的用户名"));
      }
      // 没有参数表示，没有错误
      callback();
    };

    // 验证密码
    const checkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入密码"));
      } else if (!/^\w{6,}$/.test(value)) {
        return callback(new Error("密码至少6位数"));
      }
      // 没有参数表示，没有错误
      callback();
    };

    // 验证重复密码
    const checkChkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入重复密码"));
      } else if (value !== this.form.password) {
        return callback(new Error("两次密码输入不一致"));
      }
      // 没有参数表示，没有错误
      callback();
    };

    // 验证手机
    const checkMobile = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入手机"));
      } else if (!/^1\d{10}$/.test(value)) {
        return callback(new Error("请输入有效的手机"));
      }
      // 没有参数表示，没有错误
      callback();
    };

    return {
      form: {},
      rules: {
        username: { validator: checkUsername },
        password: { validator: checkPassword },
        chkpassword: { validator: checkChkPassword },
        mobile: { validator: checkMobile },
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$http
            .post("admin/create", {
              username: this.form.username,
              password: md5(this.form.password),
              mobile: this.form.mobile,
            })
            .then((res) => {
              if (res.result.err > 0) {
                this.$message.error(res.result.msg);
              } else {
                this.$message.success(res.result.msg);
                setTimeout(() => this.$router.push("/login"), 100);
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="less">
.login {
  display: flex;
  height: 100vh;
  flex-direction: column;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 10%;
    margin-bottom: 7px;
    div:nth-child(1) {
      margin-left: 110px;
      font-size: 28px;
      font-weight: bold;
      color: #2e2e2c;
      img {
        vertical-align: middle;
      }
    }
    div:nth-child(2) {
      margin-right: 346px;
      font-size: 16px;
      font-weight: 400;
      color: #999999;
    }
  }
  .main {
    display: flex;
    position: relative;
    height: 80%;
    background: url(../assets/images/banner.png) no-repeat;
    background-size: 100% 100%;
    .form {
      position: absolute;
      margin: 0 auto;
      width: 21%;
      height: 59%;
      right: 20%;
      top: 20%;
      background: #ffffff;
      padding: 30px;
      box-shadow: 0px 0px 29px 0px rgba(3, 57, 99, 0.41);
      border-radius: 5px;
      .forget {
        position: absolute;
        right: 0;
        top: 0;
        font-size: 14px;
        font-weight: 400;
        color: #999999;
      }

      h2 {
        font-size: 18px;
        font-weight: bold;
        color: #333333;
        text-align: center;
        margin-bottom: 41px;
      }
      img {
        position: absolute;
        top: 11px;
        right: 10px;
      }
    }
  }
  .footer {
    display: flex;
    align-items: center;
    height: 10%;
    margin: 0 auto;
    div:nth-child(1) {
      margin-right: 21px;
      img {
        vertical-align: -1px;
      }
      font-size: 12px;
      font-weight: 400;
      color: #999999;
    }
    div:nth-child(2) {
      margin-right: 21px;
      img {
        vertical-align: -1px;
      }
      font-size: 12px;
      font-weight: 400;
      color: #999999;
    }
    div:nth-child(3) {
      img {
        vertical-align: -1px;
      }
      font-size: 12px;
      font-weight: 400;
      color: #999999;
    }
  }
}
</style>
